package com.example.admin.yuejian21.activity;

import android.content.DialogInterface;
import android.content.Intent;
import android.support.constraint.solver.SolverVariable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.ImageButton;
import android.widget.RadioButton;
import android.widget.TextView;
import android.widget.Toast;

import com.example.admin.yuejian21.Bean.Club;
import com.example.admin.yuejian21.R;
import com.example.admin.yuejian21.Service.Service;
import com.example.admin.yuejian21.fragment.ActivityFragment;
import com.example.admin.yuejian21.fragment.ContestFragment;
import com.example.admin.yuejian21.fragment.CourseFragment;
import com.example.admin.yuejian21.fragment.IntroFragment;
import com.example.admin.yuejian21.fragment.MasterFragment;
import com.example.admin.yuejian21.fragment.VideoFragment;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
import retrofit2.http.Query;

import static com.example.admin.yuejian21.Adapter.RenqiAdapter.id;
import static com.example.admin.yuejian21.activity.LoginActivity.userid;

public class ClubDetailActivity extends AppCompatActivity {
    private RadioButton introButton;
    private RadioButton courseButton;
    private RadioButton videoButton;
    private IntroFragment introFragment;
    private CourseFragment courseFragment;
    private VideoFragment videoFragment;
    private FragmentManager fragmentManager;
    private FragmentTransaction transaction;
    private Club club;
    private TextView nameview;
    private ImageButton cencernButton;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_club_detail);
        introButton= (RadioButton) findViewById(R.id.Button_intro);
        courseButton= (RadioButton) findViewById(R.id.Button_course);
        videoButton= (RadioButton) findViewById(R.id.Button_video);
        nameview= (TextView) findViewById(R.id.clubtextView);
        cencernButton= (ImageButton) findViewById(R.id.imageView_concern);
        club=getIntent().getParcelableExtra("club");
        nameview.setText(club.getClub_name());
        getcollectestate( userid,id,"1");

        cencernButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dofavor(userid,id,"1");

                    cencernButton.setImageResource(R.drawable.ic_guanzhu1);
                   }




        });


        getcollectestate( userid,id,"1");



        introButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if(isChecked){
                  introFragment=new IntroFragment();
                    replacefragment(introFragment);

                }
            }
        });


        courseButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if(isChecked){
                   courseFragment=new CourseFragment();
                    replacefragment(courseFragment);

                }
            }
        });
        videoButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if(isChecked){
                   videoFragment=new VideoFragment();
                    replacefragment(videoFragment);

                }
            }
        });

        fragmentManager=getSupportFragmentManager();
        introButton.setChecked(true);

    }
    public void getcollectestate(String userid, String fan_id, String type){

        Retrofit retrofit = new Retrofit.Builder().baseUrl("http://172.24.10.175/workout/api.php/").addConverterFactory(GsonConverterFactory.create())
                .build();
        Service service=retrofit.create(Service.class);
        Call<Integer> call=service.isconcern(userid,fan_id,type);

        call.enqueue(new Callback<Integer>() {
            @Override
            public void onResponse(Call<Integer> call, Response<Integer> response) {

                if(response.body()!=0)
                {
                    cencernButton.setImageResource(R.drawable.ic_guanzhu);

                }
                else {
                    cencernButton.setImageResource(R.drawable.ic_guanzhu1);



                } ;

            }

            @Override
            public void onFailure(Call<Integer> call, Throwable t) {

            }
        });

    }
    private void dofavor(String userId, String clubId,String type){

        Retrofit retrofit = new Retrofit.Builder().baseUrl("http://172.24.10.175/workout/api.php/").addConverterFactory(GsonConverterFactory.create())
                .build();
        Service service=retrofit.create(Service.class);
        Call<Integer> call=service.concern(userId,clubId,type);
        call.enqueue(new Callback<Integer>() {
            @Override
            public void onResponse(Call<Integer> call, Response<Integer> response) {
                if (response.body() != 0) {
                    Toast.makeText(ClubDetailActivity.this, "已关注", Toast.LENGTH_SHORT).show();

                }
            }
            public void onFailure(Call<Integer> call, Throwable t) {

            }
        });



    }
    private  void replacefragment(Fragment fragment){
        transaction=fragmentManager.beginTransaction();
        transaction.replace(R.id.fragment_holder,fragment);
        transaction.commit();
    }
}
